<!DOCTYPE html>
<html lang="en">
<!--js 的终结 https://github.com/cy0707/Learn_JavaScript/issues/16-->
<head>
    <meta charset="UTF-8">
    <title>长连接</title>
    <link rel="stylesheet" type="text/css" href="../normalize.css">
    <style>

    </style>
    <script src="../jquery-2.1.4.min.js"></script>
    <script>
        function souceEventTest(){
            //../../../wordpress/comet.php 的内容和source-event.php相同
            var source = new EventSource('../../../wordpress/comet.php', { withCredentials: true });
            source.onopen = function (event) {
                console.log("创建了一个长链接")
            };
            source.onerror = function (event) {
                console.log(event)
            };
            source.addEventListener('ping',function (event) {
                let e = event || window.event;
                console.log(e.data);
            },false);
            source.onmessage = function (event) {
                let e = event || window.event;
                console.log(e.data);
            };
        }

        $(document).ready(function () {
           souceEventTest();
        })
    </script>
</head>
<body>
<div>
    <span>长连接测试</span>
</div>


</body>
</html>